﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.IO;

namespace IconManager.ObjectModel
{
	/// <summary>
	/// Static methods to return collections of UmbracoIcons / PackIcons and Pack names
	/// </summary>
	public static class Icons
	{
		/// <summary>
		/// Gets the Umbraco folder where the active icons are stored
		/// </summary>
		internal static DirectoryInfo UmbracoIconDir 
		{
			get 
			{ 
				return new DirectoryInfo(HttpContext.Current.Server.MapPath(UmbracoIcon.UmbracoIconDirUrl));
			}
		}

		/// <summary>
		/// Gets the base dir where the icon pack folders are stored
		/// </summary>
		private static DirectoryInfo PacksRootDir 
		{
			get
			{
				return new DirectoryInfo(HttpContext.Current.Server.MapPath(PackIcon.PacksRootDirUrl));
			}
		}

		/// <summary>
		/// Returns all Icon Packs
		/// (each folder in umbraco/plugins/IconManger/IconCollections/ represents an IconPack)
		/// </summary>
		/// <returns>a list folder names</returns>
		public static List<string> GetPacks()
		{
			List<string> packs = new List<string>();

			foreach (DirectoryInfo packDir in Icons.PacksRootDir.GetDirectories())
			{
				packs.Add(packDir.Name);
			}

			return packs;
		}

		/// <summary>
		/// Gets all icons in an IconPack
		/// </summary>
		/// <param name="packName">The pack folder name</param>
		/// <returns>a collection</returns>
		public static List<PackIcon> GetPackIcons(string packName)
		{
			List<PackIcon> packIcons = new List<PackIcon>();

			DirectoryInfo packDir = Icons.PacksRootDir.GetDirectories(packName, SearchOption.TopDirectoryOnly).FirstOrDefault();
			if (packDir != null)
			{
				// Get all *.png files
				foreach (FileInfo pngFile in packDir.GetFiles("*.png", SearchOption.TopDirectoryOnly))
				{
					packIcons.Add(new PackIcon(packName, pngFile.Name));
				}

				// Get all *.gif files
				foreach (FileInfo gifFile in packDir.GetFiles("*.gif", SearchOption.TopDirectoryOnly))
				{
					packIcons.Add(new PackIcon(packName, gifFile.Name));
				}
			}

			return packIcons;
		}

		/// <summary>
		/// Gets all icons visible to Umbraco
		/// </summary>
		/// <returns>a collection</returns>
		public static List<UmbracoIcon> GetUmbracoIcons()
		{
			List<UmbracoIcon> umbracoIcons = new List<UmbracoIcon>();

			foreach (FileInfo pngFile in Icons.UmbracoIconDir.GetFiles("*.png", SearchOption.TopDirectoryOnly))
			{
				if (pngFile.Name != "sprites.png") // Exclude this file... or perhaps ensure that any files added are of 16x16 ? (slow)
				{
					umbracoIcons.Add(new UmbracoIcon(pngFile.Name));
				}
			}

			// Get all *.gif files
			foreach (FileInfo gifFile in Icons.UmbracoIconDir.GetFiles("*.gif", SearchOption.TopDirectoryOnly))
			{
				if (gifFile.Name != "sprites_ie6.gif")
				{
					umbracoIcons.Add(new UmbracoIcon(gifFile.Name));
				}
			}


			return umbracoIcons;
		}
	}
}